<template>
  <div class="user">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="用户">
        <el-input v-model="formInline.user" placeholder="用户" />
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker
          v-model="formInline.date"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="table">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" width="50" />
        <el-table-column prop="username" label="账号" width="160" />
        <el-table-column prop="nickname" label="名称" width="160" />
        <el-table-column prop="sex" label="性别" width="80" />
        <el-table-column prop="age" label="年龄" width="80" />
        <el-table-column prop="phone" label="手机号" width="160">
          <template slot-scope="scope">
            {{ phoneFormat(scope.row.phone) }}
          </template>
        </el-table-column>
        <el-table-column prop="prov" label="所在地">
          <template slot-scope="scope">
            {{ scope.row.prov }} - {{ scope.row.city }}
          </template>
        </el-table-column>
        <el-table-column prop="date" label="时间" width="180">
          <template slot-scope="scope">
            {{ formData(scope.row.date) }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="禁用标识" width="140">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.state" type="danger" size="small">
              已禁用
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-link
              type="primary"
              :underline="false"
              style="margin-right: 15px"
            >
              详情
            </el-link>
            <el-link
              v-if="scope.row.username != 'admin'"
              :type="scope.row.state ? 'success' : 'danger'"
              :underline="false"
              @click="disableUser(scope.row)"
            >
              {{ scope.row.state ? "恢复" : "禁用" }}
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  name: 'User',
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      formInline: {
        user: '',
        date: ''
      },
      tableData: []
    }
  },
  created() {
    this.getUsers()
  },
  mounted() {
    // this.getUsers()
  },
  methods: {
    // 手机号格式
    phoneFormat(val) {
      if (!val) return ''
      const phoneArr = val.split('')
      phoneArr.splice(3, 4, '****')
      return phoneArr.join('')
    },

    disableUser(data) {
      let stateDesc = '禁用'
      if (data.state) stateDesc = '恢复'

      this.$confirm('此操作将' + stateDesc + '该账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const users = await this.$localStorage.getItem('allAccount')

          const newUsers = users.map((item) => {
            if (item.username === data.username) {
              item.state = !item.state
            }
            return item
          })
          await this.$localStorage.setItem('allAccount', newUsers)
          this.getUsers()
          this.$message({
            type: 'success',
            message: '禁用成功!'
          })
        })
        .catch(() => {})
    },
    async getUsers() {
      const data = await this.$localStorage.getItem('allAccount')
      if (data) {
        this.tableData = data
      }
    },
    formData(date) {
      if (!date) return
      const d = new Date(date)
      const year = d.getFullYear()
      const month = d.getMonth() + 1
      const day = d.getDate()
      const hour = d.getHours()
      const minute = d.getMinutes()
      const second = d.getSeconds()
      return (
        year +
        '-' +
        month +
        '-' +
        day +
        ' ' +
        this.zeroFill(hour) +
        ':' +
        this.zeroFill(minute) +
        ':' +
        this.zeroFill(second)
      )
    },
    zeroFill(time) {
      if (time < 10) {
        return '0' + time
      } else {
        return time
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.user {
  padding: 20px 100px;
  .table {
  }
}
</style>

